// PSFileGen.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <string>
#include <vector>

#include <d3d11.h>
#include <d3dx11.h>
#include "../racing-game/Effects11/Inc/d3dx11effect.h"
#include <dxerr.h>

#if defined(DEBUG) | defined(_DEBUG)
	#ifndef HR
	#define HR(x)                                              \
	{                                                          \
		HRESULT hr = (x);                                      \
		if(FAILED(hr))                                         \
		{                                                      \
			DXTrace(__FILE__, (DWORD)__LINE__, hr, #x, true); \
		}                                                      \
	}
	#endif

#else
	#ifndef HR
	#define HR(x) (x)
	#endif
#endif 

#define ReleaseCOM(x) { if(x){ x->Release();x = 0; } }


struct Define
{
	Define(const std::string & name, const std::string & value)
	{ Name = name; Value = value; }
	std::string Name, Value;
};

struct ShaderDesc
{
	ShaderDesc(const std::string & file) { File = file; }
	std::string File;
	std::vector<Define> Defines;
};


int _tmain(int argc, _TCHAR* argv[])
{
	ID3D11Device*           device;
	ID3D11DeviceContext*    context;

	D3D_FEATURE_LEVEL mFeatureLevel = D3D_FEATURE_LEVEL_11_0;

	UINT createDeviceFlags = 0;
	createDeviceFlags |= D3D10_CREATE_DEVICE_SINGLETHREADED;
#if defined(DEBUG) || defined(_DEBUG)  
	createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
#endif

	D3D_FEATURE_LEVEL tmpFeatureLevel;
	HR ( D3D11CreateDevice( NULL,                        // Use default graphics card
                            D3D_DRIVER_TYPE_HARDWARE,    // Try to create a hardware accelerated device
                            NULL,                        // Do not use external software rasterizer module
                            createDeviceFlags,              // Device creation flags
                            &mFeatureLevel,
                            1,
                            D3D11_SDK_VERSION,           // SDK version
                            &device,                 // Device out
                            &tmpFeatureLevel,                      // Actual feature level created
                            &context ) );              // Context out

	std::vector<ShaderDesc> shadersToLoad;
	shadersToLoad.push_back(ShaderDesc("ShadowMap.fx"));

	ID3DX11Effect * effect;
	std::string path = "data\\";
	path += "ShadowMap.fx";

	DWORD shaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
	shaderFlags |= D3D10_SHADER_DEBUG;
	shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION;
#else
	shaderFlags |= D3D10_SHADER_OPTIMIZATION_LEVEL3;
#endif

	D3D10_SHADER_MACRO * macros = NULL;
	//if(defines.size() > 0)
	//{
	//	macros = new D3D10_SHADER_MACRO[defines.size() + 1];
	//	for(uint i = 0; i < defines.size(); i++)
	//	{
	//		macros[i].Name = defines[i].Name.c_str();
	//		macros[i].Definition = defines[i].Definition.c_str();
	//	}
	//	macros[defines.size()].Name = NULL;
	//	macros[defines.size()].Definition = NULL;
	//}
		
	ID3D10Blob *dataBlob = 0, *errorBlob = 0;
	HRESULT hr = D3DX11CompileFromFile(path.c_str(), macros, NULL, NULL, "fx_5_0", shaderFlags, 0, NULL, &dataBlob, &errorBlob, NULL);
	if (FAILED(hr))
	{
		if( errorBlob )
		{
			MessageBoxA(0, (char*)errorBlob->GetBufferPointer(), 0, 0);
			ReleaseCOM(errorBlob);
		}
		DXTrace(__FILE__, (DWORD)__LINE__, hr, path.c_str(), true);
		return NULL;
	}
	delete macros;
		
	HR(D3DX11CreateEffectFromMemory(dataBlob->GetBufferPointer(), dataBlob->GetBufferSize(), 0, device, &effect));

	D3DX11_EFFECT_DESC efDesc;
	effect->GetDesc(&efDesc);

	//effect->GetVariableByIndex(0)->

	ReleaseCOM(effect);

	context->ClearState();

	ReleaseCOM(context);
	ReleaseCOM(device);

	return 0;
}

